package com.example.game_app;

import androidx.appcompat.app.AppCompatActivity;

import android.animation.ValueAnimator;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.app.Activity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivityAnimation extends AppCompatActivity {
    private TextView textView;
    private String textToType = "onego游戏中心"; // 你想显示的文本
    private int currentIndex = 0;

    int[] colors = new int[]{0x990066ff, 0x770066ff, 0x550066ff};
    private ImageView img;

    private ProgressBar pbIndex;// 对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_animation);
        img = (ImageView) findViewById(R.id.Imageview01);
        textView = findViewById(R.id.tv_typewriter);
        pbIndex = findViewById(R.id.pbIndex);
        startTypingAnimation();
        // 从哪种透明度到哪种透明度(1完全不透明，0完全透明)
        AlphaAnimation animation = new AlphaAnimation(0, 1);
        // 设置动画执行时间毫秒
        animation.setDuration(2000);
        img.startAnimation(animation);
        toTiaoguo(findViewById(R.id.pbIndex));
    }

    // 页面重新显示时，重新运行 开机动画
    @Override
    protected void onRestart() {
        setContentView(R.layout.activity_main_animation);
        img = (ImageView) findViewById(R.id.Imageview01);
        textView = findViewById(R.id.tv_typewriter);
        pbIndex = findViewById(R.id.pbIndex);
        startTypingAnimation();
        // 从哪种透明度到哪种透明度(1完全不透明，0完全透明)
        AlphaAnimation animation = new AlphaAnimation(0, 1);
        // 设置动画执行时间毫秒
        animation.setDuration(2000);
        img.startAnimation(animation);
        toTiaoguo(findViewById(R.id.pbIndex));
        super.onRestart();
    }

    private void startTypingAnimation() {
        ValueAnimator animator = ValueAnimator.ofInt(0, textToType.length());
        animator.setDuration(1500); // 总动画时长为3秒，根据需求调整
        animator.setInterpolator(new LinearInterpolator()); // 线性插值器
        animator.addUpdateListener(animation -> {
            currentIndex = (int) animation.getAnimatedValue();
            SpannableString spannableString = new SpannableString(textToType.substring(0, currentIndex));
            // 定义颜色数组
            int[] colors = {0xFFD95050, 0xFFD9B250, 0xFFD4D950, 0xFF50D99E, 0xFF50D9CE, 0xFF50B9D9, 0xFF9050D9, 0xFFB450D9, 0xFFD950A7};
            // 为每个显示的字符设置颜色
            for (int i = 0; i < currentIndex; i++) {
                int colorIndex = i % colors.length; // 循环使用颜色数组
                spannableString.setSpan(new ForegroundColorSpan(colors[colorIndex]), i, i + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            // 更新TextView的文本和颜色
            textView.setText(spannableString);
        });
        animator.start();
    }


    public void toTiaoguo(View view) {
        new Thread() {
            public void run() {
                pbIndex.setMax(10);
                int i = 0;
                while (i < 10) {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

                    pbIndex.setProgress(i);
                    i++;
                }
                // 跳转 意图
                Intent intent = new Intent(MainActivityAnimation.this, LoginActivity.class);
                // 运行意图
                startActivity(intent);
            }
        }.start();

    }


}